//------------------------------------------------------------------------------
///	\file event_log.c
///
///	\brief
///	Ring Buffer implementation file
///
///	This is a simple event logging mechanism that can be used
///	to track the progress of an algorithm.
///
//	$Id: event_log.c 350 2015-02-26 18:25:37Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	The MIT License (MIT)
//
//	Copyright (c) 2014 A.C. Verbeck
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.

//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//------------------------------------------------------------------------------
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

#include "event_log.h"

//------------------------------------------------------------------------------
//	Local data
//------------------------------------------------------------------------------
static uint8_t	event_log[EVENT_LOG_SZ];										//	event storage location
static uint32_t	event_log_idx=0;												//	event log index
static uint32_t	event_log_total=0;												//	total events tracked

//------------------------------------------------------------------------------
///	\brief
///		Event log initialization
//------------------------------------------------------------------------------
void EventLog_init(void)
{
	event_log_idx = 0;															//	clear index
	event_log_total = 0;														//	clear total
	memset(event_log, 0, sizeof(event_log));									//	clear event log
}

//------------------------------------------------------------------------------
///	\brief
///		Log an event
///
/// \param	[in]	ev		event to be logged
//------------------------------------------------------------------------------
void EventLog_log(uint8_t ev)
{
	event_log[event_log_idx++] = ev;											//	store event
	if (event_log_idx == EVENT_LOG_SZ) event_log_idx = 0;						//	bump index, roll if necessary
	event_log_total+=1;															//	bump total
}

//------------------------------------------------------------------------------
///	\brief
///		Dump the event log buffer
//------------------------------------------------------------------------------
void EventLog_dump(void)
{
	uint32_t i;
	
	printf("Total Events: %d\n",event_log_total);								//	print total events
	printf("Event Index: %d\n",event_log_idx);									//	print event index
	printf("      00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f");			//	print data header
	for (i=0; i<EVENT_LOG_SZ; i++) {											//	loop through data
		if ((i % 16) == 0)	printf("\n%04x: ", i);								//	print data sidebar
		printf("%02x ", event_log[i]);											//	print data
	}
	printf("\n");																//	all done
}

//
//	End event_log.c
//
